package com.army.stock.web;

import com.army.stock.service.ArmyDepartmentService;
import com.army.stock.service.ArmyMonthplanService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangyong on 15/5/18.
 */
@Controller
public class MonthplansummaryController {

    private final static Logger logger = LoggerFactory.getLogger(MonthplansummaryController.class);

    @Autowired
    private ArmyMonthplanService armyMonthplanService;

    @Autowired
    private ArmyDepartmentService armyDepartmentService;

    @RequestMapping(value = "/monthplanssummary", method = RequestMethod.GET)
    public String monthplanssummary(HttpServletRequest request, ModelMap modelMap) {
        String departmentId = request.getParameter("departmentId");
        String year = request.getParameter("year");
        String month = request.getParameter("month");
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isNotEmpty(departmentId)) {
            map.put("departmentId", departmentId);
        }
        Calendar calendar = Calendar.getInstance();
        if (StringUtils.isBlank(year) || StringUtils.isBlank(month)) {
            year = String.valueOf(calendar.get(Calendar.YEAR));
            month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
        }
        int currentYear = calendar.get(Calendar.YEAR);
        int nextYear = currentYear + 1;
        map.put("year", year);
        map.put("month", month);
        logger.info("departmentId: year: month:", map);
        List page = armyMonthplanService.getmonthplanssummary(map);
        modelMap.put("page", page);
        modelMap.put("year", year);
        modelMap.put("month", month);
        modelMap.put("currentYear", currentYear);
        modelMap.put("nextYear", nextYear);
        List departmentList = armyDepartmentService.getAllArmyDepartments();
        modelMap.put("departmentList", departmentList);
        BigDecimal subtotal = armyMonthplanService.getSummonthplanssummary(map);
        modelMap.put("subtotal" ,subtotal);
        return "monthplan/monthPlansummary";
    }

}
